package com.ysm.smfu.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.umeng.analytics.MobclickAgent;
import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.StatusBarCompat;
import com.ysm.smfu.welcome.WelcomeActivity;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 1. 将相同的方法抽取出来
 * 2. 统一管理所有的acitivity
 */
public class BaseActivity extends AutoLayoutActivity implements View.OnClickListener {

    // 共享资源
    protected static List<BaseActivity> activities = new ArrayList<>();
    public static BaseActivity activity;//当前的Activity
    protected Bundle savedInstanceState;
    private boolean isSuit = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;

        //添加到集合中统一维护
        synchronized (activities) {
            activities.add(this);
        }
        init();
        initViews();
        setIsSuitStatuBar();
        initData();
        initToolBar();
        initListener();
        dealCommon();
    }

    /**
     * 不需要状态栏颜色
     */
    protected void setNoSuitStatuBar() {
        isSuit = false;
    }

    /**
     * 是否实现沉浸式状态栏
     */
    protected void setIsSuitStatuBar() {
        if (isSuit)
            //        实现沉浸式状态栏
            StatusBarCompat.compat(this, getResources().getColor(R.color.green));
    }

    @Override
    protected void onResume() {
        super.onResume();
        activity = this;
        beforeOnResumeInitMobclickAgent();
        MobclickAgent.onResume(this);
        afterResume();
    }

    protected void beforeOnResumeInitMobclickAgent() {
        MobclickAgent.onPageStart(getLocalClassName());
    }

    protected void afterResume() {
    }

    @Override
    protected void onPause() {
        super.onPause();
        activity = null;
        beforeOnPauseInitMobclickAgent();
        MobclickAgent.onPause(this);
        afterPause();
    }

    protected void beforeOnPauseInitMobclickAgent() {
        MobclickAgent.onPageEnd(getLocalClassName());
    }

    private void afterPause() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        synchronized (activities) {
            activities.remove(this);
            if (activities.size() == 0) {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);

    }

    public void killAll() {
        //遍历中不允许增删
        //1. 复制一份
        //2. CopyOnWriteArrayList 可以在遍历中做增删操作
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            activity.finish();
        }
//        保存友盟数据
        MobclickAgent.onKillProcess(this);
//          自杀进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public void killOthersElseLogin() {
        //遍历中不允许增删
        //1. 复制一份
        //2. CopyOnWriteArrayList 可以在遍历中做增删操作
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            if (!(activity instanceof All_LoginActivity))
                activity.finish();
        }
    }

    public void killOthersElseMainActivty() {
        //遍历中不允许增删
        //1. 复制一份
        //2. CopyOnWriteArrayList 可以在遍历中做增删操作
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            if (!(activity instanceof MainActivity))
                activity.finish();
        }
    }

    public void killOthersElseMainActivtyAndLogin() {
        //遍历中不允许增删
        //1. 复制一份
        //2. CopyOnWriteArrayList 可以在遍历中做增删操作
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            if (!(activity instanceof All_LoginActivity) && !(activity instanceof MainActivity))
                activity.finish();
        }
    }

    public void killWecPage() {
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            if (activity instanceof WelcomeActivity)
                activity.finish();
        }
    }

    /**
     * 初始化操作
     */
    protected void init() {

    }

    /**
     * 初始化所有的控件
     */
    protected void initViews() {

    }

    /**
     * 填充控件
     */
    protected void initData() {
    }

    /**
     * 初始化toolbar
     */
    protected void initToolBar() {

    }

    /**
     * 初始化监听事件和设置adapter
     */
    protected void initListener() {
    }

    /**
     * 对后退按键统一处理
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                //关闭当前界面

                finish();
                // 设置Activity的切换效果
//                overridePendingTransition(R.anim.in_from_left,
//                        R.anim.out_to_right);
                break;

            default: // 非返回按钮
                onInnerClick(v);
                break;
        }
    }

    /**
     * 非后退控件的点击事件
     *
     * @param v
     */
    public void onInnerClick(View v) {

    }

    private void dealCommon() {
        //限定子类的返回按钮id 必须是back
        View back = findViewById(R.id.back);
        //子Activity不一定有返回按钮
        if (back != null)
            back.setOnClickListener(this);
    }
}
